package com.mall.cart.vo;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车对象
 */
public class Cart {

    private List<CartItem> items;

    /**
     * 数量
     */
    private Integer count;

    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 商品总价
     */
    private BigDecimal amount;
    /**
     * 优惠金额
     */
    private BigDecimal reduce;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCount() {
        if (CollectionUtils.isEmpty(items)) {
            return 0;
        }
        return items.stream().mapToInt(CartItem::getCount).sum();
    }

    public Integer getCountType() {
        if (CollectionUtils.isEmpty(items)) {
            return 0;
        }
        return items.size();
    }

    public BigDecimal getAmount() {
        if(CollectionUtils.isEmpty(getItems())){
            return BigDecimal.ZERO;
        }
        return items.stream().filter(CartItem::getCheck).map(CartItem::getTotalPrice).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).subtract(getReduce());
    }

    public BigDecimal getReduce() {
        return BigDecimal.ZERO;
    }
}
